﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SeDuce.IDAL;
using System.Data.SqlClient;

namespace SeDuce.DAL
{
    public class NewsTypeDAL : INewsTypeDAL
    {
        #region SQL
        public const string sqlGetNewsTypes = "SELECT * FROM TbNewsType";
        const string sqlUpdateNewsType = "UPDATE TbNewsType SET TypeName = {0} WHERE (ID = {1})";
        const string sqlInsertNewsType = "INSERT INTO TbNewsType (TypeName) VALUES ({0})";
        const string sqlDeleteNewsType = "Delete from TbNewsType where ID={0}";
        #endregion

        #region Variables
        private static DataAccessObjectBase _dao = null;
        public static DataAccessObjectBase DAO
        {
            get
            {
                if (_dao == null)
                    _dao = new DataAccessObjectBase();
                return _dao;
            }
            set
            {
                _dao = value;
            }
        }
        #endregion

        System.Data.DataTable INewsTypeDAL.GetNewsType()
        {
            return DAO.Select(sqlGetNewsTypes);
        }

        bool INewsTypeDAL.UpdateNewsType(Model.NewsType newsType)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@TypeName", newsType.TypeName),
                new SqlParameter ("@ID", newsType.ID)
            };
            string sql = string.Format(sqlUpdateNewsType, "@TypeName", "@ID");

            return DAO.ExecuteSql(sql, sp);
        }

        bool INewsTypeDAL.InsertNewsType(Model.NewsType newsType)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@TypeName", newsType.TypeName)
            };
            string sql = string.Format(sqlInsertNewsType, "@TypeName");

            return DAO.ExecuteSql(sql, sp);
        }

        bool INewsTypeDAL.DeleteNewsType(int id)
        {
            SqlParameter[] sp = new SqlParameter[] { 
                new SqlParameter ("@ID",id),
    
            };
            string sql = string.Format(sqlDeleteNewsType, "@ID");
            return DAO.ExecuteSql(sql, sp);
        }
    }
}
